 <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="static/easyui/jquery.min.js"></script>
<script type="text/javascript" src="static/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="static/easyui/locale/easyui-lang-zh_CN.js"></script>
<link rel="stylesheet" href="static/easyui/themes/metro-gray/easyui.css"/>
<link rel="stylesheet" href="static/easyui/themes/icon.css"/>
<title>用户管理</title>
</head>
<body> 
<script type="text/javascript">
$(function(){
	$.extend($.fn.validatebox.defaults.rules, {
   	 brage:{
             validator: function(value, param){
               //自定义正则验证,密码验证
               var reg = /^[1-9]\d*$/;
               return reg.test(value);
             },
             message:'年龄格式错误'
       },
   });
	 
   $.extend($.fn.validatebox.defaults.rules, {
   	 brname:{
             validator: function(value, param){
               //自定义正则验证,密码验证
               var reg = /^([a-zA-Z0-9\u4e00-\u9fa5\·]{1,10})$/;
               return reg.test(value);
             },
             message:'姓名格式错误'
       },
   });
   
   $.extend($.fn.validatebox.defaults.rules, {
   	 brsfz:{
             validator: function(value, param){
               //自定义正则验证,密码验证
               var reg = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/;
               return reg.test(value);
             },
             message:'身份证号格式错误'
       },
   });
   
   $.extend($.fn.validatebox.defaults.rules, {
   	 brdh:{
             validator: function(value, param){
               //自定义正则验证,密码验证
               var reg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
               return reg.test(value);
             },
             message:'电话号码格式错误'
       },
   });
   
   $.extend($.fn.validatebox.defaults.rules, {
   	 brdz:{
             validator: function(value, param){
               //自定义正则验证,密码验证
               var reg = /^[\u4E00-\u9FA5A-Za-z0-9_]+$/;
               return reg.test(value);
             },
             message:'家庭住址格式错误'
       },
   });
});

//时间转换
function comformatDate(date) {
	var day = new Date(date); 
	var Year = 0; 
	var Month = 0; 
	var Day = 0; 
	var CurrentDate = ""; 
	//初始化时间 
	Year= day.getFullYear();//
	Month= day.getMonth()+1; 
	Day = day.getDate(); 
	Hour = day.getHours(); 
	 Minute = day.getMinutes(); 
	 Second = day.getSeconds(); 
	CurrentDate += Year + "-"; 
	if (Month >= 10 ) { 
		CurrentDate += Month + "-"; 
	}else { 
		CurrentDate += "0" + Month + "-"; 
	} 
	if (Day >= 10 ) { 
		CurrentDate += Day ; 
	}else { 
		CurrentDate += "0" + Day ; 
	} 
	if(Hour>=10){
		CurrentDate +=" "+Hour;
	}else{
		CurrentDate +=" "+"0"+Hour;
	}
	if(Minute>=10){
		CurrentDate +=":"+Minute;
	}else{
		CurrentDate +=":"+"0"+Minute;
	}
	if(Second>=10){
		CurrentDate +=":"+Second;
	}else{
		CurrentDate +=":"+"0"+Second;
	}
	return CurrentDate; 
};

function yhsq(roleId){ 
	//去除表格的选中
	$("#role-list").datagrid("unselectAll");
	$("#grant-win").attr("data-value",roleId);
	//打开窗口
	$("#grant-win").window("open");
	//初始化树形结构
	$("#grant-tree").tree({
		checkbox:true,
		url:'yhsx?userId='+roleId,
		onlyLeafCheck:true
	})
}
//清除角色
function qcjs(a){
	$.messager.confirm('温馨提示', '确定清除角色吗？', function(r){
		if (r){
			$.ajax({
				url:'qcjs',
				type:'post',
				data:{
					yhid:a,
				},
				success:function(d){
					$.messager.show({
						title:'结果',
						msg:d,
						height:200,
						timeout:1000,
						showType:'slide'
					});
				}
			});
		}
	});
}
//重置密码
var bhid;
function yhmm(a){
	$.messager.confirm('温馨提示', '确定重置密码吗？', function(r){
		if (r){
			 $.ajax({
					url:'yhxg',
					type:'post',
					data:{
						yhid:a,
					},
					success:function(d){
						$.messager.show({
							title:'结果',
							msg:d,
							height:200,
							timeout:1000,
							showType:'slide'
						});

					}
				});
		}
	});
		
}
	$(function(){
		$("#xzyh").click(function(){
			$("#xzyha").window("open");	
		});
		//新增用户初始化科室下拉列表
		$('#bumen').combobox({  
			 onSelect:function(row){
				
			   if(row.value=='门诊'){
				   
				   $('#xzks').combobox({
					    url:'findMzks',//数据源
					    valueField:'departments_id',//实际值
					    textField:'departments_name',//显示值
					});
			   }else{
				   $('#xzks').combobox({
					    url:'ljw-departments',//数据源
					    valueField:'departments_id',//实际值
					    textField:'departments_name',//显示值
					});
			   }
			 }
		});
		//修改用户初始化下拉列表
		$('#xgbumen').combobox({  
			 onSelect:function(row){
			   if(row.value=='门诊'){
				   $('#xzksa').combobox({
					    url:'findMzks',//数据源
					    valueField:'departments_id',//实际值
					    textField:'departments_name',//显示值
					});
			   }else{
				   $('#xzksa').combobox({
					    url:'ljw-departments',//数据源
					    valueField:'departments_id',//实际值
					    textField:'departments_name',//显示值
					});
			   }
			 }
		});
		
		/* $('#xzksa').combobox({
		    url:'cxsyks',//数据源
		    valueField:'departments_id',//实际值
		    textField:'departments_name',//显示值
		}); */
		//确定新增用户
		$("#xzyhxq").click(function(){
			var xzname=$("#xzname").textbox("getValue");
			var xzkeshi=$("#xzks").textbox("getValue");
			var xzage=$("#xzage").textbox("getValue");
			var xzsex=$("#xzsex").textbox("getValue");
			var xzphone=$("#xzphone").textbox("getValue");
			var xzsfz=$("#xzsfz").textbox("getValue");
			var xzzt=$("#xzzt").textbox("getValue");
			$.ajax({
				url:'xzyh',
				type:'post',
				data:{
					xzname:xzname,
					xzkeshi:xzkeshi,
					xzage:xzage,
					xzsex:xzsex,
					xzphone:xzphone,
					xzsfz:xzsfz,
					xzzt:xzzt,
				},
				success:function(d){
					$.messager.show({
						title:'结果',
						msg:"新增成功！！！",
						height:200,
						timeout:1000,
						showType:'slide'
					});
					$("#yhxz").form("clear");
					$("#xzyha").window("close");
					$('#xzyha').datagrid('reload');
				}
			});
		});

		//取消新增用户
		$("#xzyhqx").click(function(){
			$("#xzname").textbox("setValue",null);
			$("#xzks").textbox("setValue",null);
			$("#xzage").textbox("setValue",null);
			$("#xzsex").textbox("setValue",null);
			$("#xzphone").textbox("setValue",null);
			$("#xzsfz").textbox("setValue",null);
			$("#xzzt").textbox("setValue",null);
			$("#bumen").textbox("setValue",null);
		});
		//取消修改用户信息
		$("#xiugqx").click(function(){
			$("#qxa").window("close");

		});
		//修改密码
		$("#qrxgmm").click(function(){
			var pwd=$("#pwd").textbox("getValue");
			var pwds=$("#pwds").textbox("getValue");
			if(pwds.length>5){
				$.ajax({
					url:'xgmm',
					type:'post',
					data:{
						yhid:bhid,
						pwd:pwd,
						pwds:pwds,
					},
					success:function(d){
						$("#xgmma").form("clear");
						$("#xgmm").window("close");
						$('#xgmm').datagrid('reload');
					}
				});
			}else{
				alert("密码长度不规范");
			}
		});
		//修改信息按钮
		$("#xgxx").click(function(){
			var yhid=$("#yhid").textbox("getValue");
			var name=$("#yhname").textbox("getValue");
			var tell=$("#phone").textbox("getValue");
			var zt=$("#zt").textbox("getValue");
			var xzksa=$("#xzksa").textbox("getValue");
			
			$.ajax({
				url:'xgxx',
				type:'post',
				data:{
					yhid:yhid,
					xzksa:xzksa,
					yhname:name,
					phone:tell,
					zt:zt,
				},
				success:function(d){
					$.messager.show({
						title:'结果',
						msg:"成功了",
						height:200,
						timeout:1000,
						showType:'slide'
					});
					$("#qxa").window("close");
					$('#qxa').datagrid('reload');
					$('#qxa').datagrid('reload');
				}
			});
		});
		
		//获取页面的二级功能id
	    var jq = top.jQuery;
	    var funId=jq("#work").tabs("getSelected").panel('options').id;
		//初始化三级的功能按钮
		var btns = function(value,row,index){
			var str="";
			$.ajax({
				url:'init-thirdfuns',
				type:'post',
				data:{
					funId:funId//参数为2级的功能id
				},
				async:false,//是否异步刷新
				success:function(data){
					for (var i = 0; i < data.length; i++) {
						str += "<input type='button' style='background:#E4F1FB;color:#2779AA;border:none' onClick='"+data[i]['function_url']+"("+row.yhid+")' value='"+data[i]['function_name']+"'/>";
					}
				}
			})
			return str;
		}
		
		
		
		//访问数据源、初始化表格
		$('#thsy').datagrid({
			url:'yhgl',
			 onLoadSuccess: function (data) {
		            if (data.total == 0) {
		                //添加一个新数据行，第一列的值为你需要的提示信息，然后将其他列合并到第一列来，注意修改colspan参数为你columns配置的总列数
		                $(this).datagrid('appendRow', { yhid: '<div style="text-align:center;color:red">没有相关记录！</div>' }).datagrid('mergeCells', { index: 0, field: 'yhid', colspan: 9 })
		                //隐藏分页导航条，这个需要熟悉datagrid的html结构，直接用jquery操作DOM对象，easyui datagrid没有提供相关方法隐藏导航条
		                $(this).closest('div.datagrid-wrap').find('div.datagrid-pager').hide();
		            }
		            //如果通过调用reload方法重新加载数据有数据时显示出分页导航容器
		            else $(this).closest('div.datagrid-wrap').find('div.datagrid-pager').show();
		        },
			columns:[[

				{field:'yhid',title:'编号',width:70},
				{field:'yhname',title:'用户姓名',width:70},
				{field:'yhks',title:'科室',width:70,align:'right'},
				{field:'sex',title:'性别',width:40,align:'right'},
				{field:'zt',title:'状态',width:50,align:'right',
					formatter:function(value){
						if(value==1){
							return "在职";
						}
						return "离职";
					}	
				},
				{field:'time',title:'入职时间',width:140,align:'right',formatter:comformatDate},
				{field:'phone',title:'电话号码',width:100,align:'right'},
				{field:'sfz',title:'身份证号',width:150,align:'right'},
				{field:'cz',title:'操作*',width:250,
					formatter:btns
				}
			]]
			});
		
		//名字查询
		$('#s-dname2').searchbox({
			searcher:function(value){
			    alert(value);
			    	   $('#thsy').datagrid({
		    				url:'mhcxyh',
		    				queryParams:{
		    					nameq:value
		    				},
		    				 onLoadSuccess: function (data) {
		    			            if (data.total == 0) {
		    			                //添加一个新数据行，第一列的值为你需要的提示信息，然后将其他列合并到第一列来，注意修改colspan参数为你columns配置的总列数
		    			                $(this).datagrid('appendRow', { yhid: '<div style="text-align:center;color:red">没有相关记录！</div>' }).datagrid('mergeCells', { index: 0, field: 'yhid', colspan: 9 })
		    			                //隐藏分页导航条，这个需要熟悉datagrid的html结构，直接用jquery操作DOM对象，easyui datagrid没有提供相关方法隐藏导航条
		    			                $(this).closest('div.datagrid-wrap').find('div.datagrid-pager').hide();
		    			            }
		    			            //如果通过调用reload方法重新加载数据有数据时显示出分页导航容器
		    			            else $(this).closest('div.datagrid-wrap').find('div.datagrid-pager').show();
		    			        },
		    				columns:[[
		    					{field:'yhid',title:'编号',width:70},
		    					{field:'yhname',title:'用户姓名',width:70},
		    					{field:'yhks',title:'科室',width:70,align:'right'},
		    					{field:'sex',title:'性别',width:40,align:'right'},
		    					{field:'zt',title:'状态',width:50,align:'right',
		    						formatter:function(value){
		    							if(value==1){
		    								return "在职";
		    							}
		    							return "离职";
		    						}	
		    					},
		    					{field:'time',title:'入职时间',width:100,align:'right',formatter:comformatDate},
		    					{field:'phone',title:'电话号码',width:100,align:'right'},
		    					{field:'sfz',title:'身份证号',width:150,align:'right'},
		    					{field:'cz',title:'操作*',width:250,
		    						formatter:btns
		    					}
		    						]]
							});
			    }
			    
			  
		});
		
		
		//关闭授权窗口
		$("#close-grant").click(function(){
			$("#grant-win").window("close");
		});
		//点击保存按钮，获得角色id和所有选中功能id
		$("#save-grant").click(function(){
			var yhid=$("#grant-win").attr("data-value");
			//获取树形结构中所有选中的节点
			 var nodes = $('#grant-tree').tree('getChecked');
			console.log(nodes);
			//将所有节点的id存入数组
			var functionIds = [];
			for (var i = 0; i < nodes.length; i++) {
				//这是三级
				functionIds.push(nodes[i].id);

			}
			
			//去重
			function dedupe(array){
				 return Array.from(new Set(array));
			}
			functionIds = dedupe(functionIds) //保证一级和二级在数组中不重复
			//提交到controller
			$.ajax({
				url:'yhsqa',
				type:'post',
				data:{
					userId:yhid,
					funIdss:JSON.stringify(functionIds)
				},
				success:function(){
				}
			}); 
		});

	});
	function xiugai(a){
		alert(1234);
		$.ajax({
			url:'yhxga',
			type:'post',
			data:{
				yhid:a,
			},
			success:function(d){
				if(d==""){
					alert("失败了");
				}else{
					 
					$("#qxa").window("open");
					$("#yhid").textbox("setValue",d[0]["yhid"]);
					$("#yhname").textbox("setValue",d[0]["yhname"]);
					var aa=d[0]["zt"];
					if(aa==0){
						$("#zt").textbox("setValue","离职");
					}else{
						$("#zt").textbox("setValue","在职");
					}
					
					$("#phone").textbox("setValue",d[0]["phone"]);
					$("#xzksa").textbox("setValue",d[0]["yhks"]);
					 
					console.log(d);
				}
			}
		});
		
		
	};
</script>
	<div class="easyui-panel" data-options="fit:true">
	<p>
		<a id="xzyh" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">新增用户</a>&nbsp;&nbsp;&nbsp;
	用户查询：<input id="s-dname2" class="easyui-searchbox">
	</p>
		<table id="thsy" class="easyui-datagrid" data-options="fitColumns:true,pagination:true"  style="height:400px">
				
		</table>
<!-- 新增用户------------------------------------------------------------------------------ -->
		<div id="xzyha" class="easyui-window" title="新增用户" style="width:540px;padding-top:10px;height:300px" data-options="iconCls:'icon-save',closed:true">

			<form id="yhxz">

			<p>
					<input id="xzname"  class="easyui-textbox"  required="required"
									data-options="label:'姓名：',width:250,labelAlign:'right'">
					
					<input id="xzage"  class="easyui-textbox"  required="required"
									data-options="label:'年龄：',width:250,labelAlign:'right'">
									
			</p>
			<p>
					<input  class="easyui-textbox"  required="required" id="xzphone"
										data-options="label:'电话：',validType:'brdh',width:250,labelAlign:'right'">
					
					<input  class="easyui-textbox"  required="required" id="xzsfz"
					data-options="label:'身份证：',width:250,labelAlign:'right',validType:'brsfz'">
					
			</p>
			<p>
					
					<select class="easyui-combobox" id="xzzt" panelHeight='auto'
						data-options="label:'状态：',labelAlign:'right',width:250,editable:false">
						<option selected="selected">在职 </option>
						<option >离职</option>
					</select>
					
					<select class="easyui-combobox" id="xzsex" panelHeight='auto'
						data-options="label:'性别：',labelAlign:'right',width:250,editable:false">
						<option selected="selected">男</option>
						<option >女</option>
					</select>
			</p>
			<p>
					<select class="easyui-combobox" id="bumen" panelHeight='auto'
						data-options="label:'部门：',labelAlign:'right',width:250,editable:false">
						<option value="门诊" selected="selected">门诊</option>
						<option value="住院">住院</option>
					</select>
					
					<select class="easyui-combobox" id="xzks" panelHeight='auto'
						data-options="label:'科室：',labelAlign:'right',width:250,editable:false">
					</select>	
			</p>
							
					<a class='easyui-linkbutton' id="xzyhxq"style="margin-left:350px;">确认</a>
 					<a class='easyui-linkbutton' id="xzyhqx"style="margin-left:10px;">取消</a>
 			
			</form>
		</div>
		
		
		
		
		<!-- 修改信息-------------------------------------------------------------------------------------------------------->
		<div id="qxa" class="easyui-window" title="修改信息" style="width:540px;padding-top:15px;height:250px"
	    	data-options="iconCls:'icon-save',closed:true">
		<form id="xgxxa">

		<p>

		<p>
				<input id="yhid" class="easyui-textbox" readonly="true" required="required"
					data-options="label:'编号：',width:250,labelAlign:'right'">
				<input id="yhname" class="easyui-textbox" readonly="true" required="required"
				data-options="label:'姓名：',width:250,labelAlign:'right'">
		</p>
		<p>
			<!-- <input id="yhid" class="easyui-textbox"  readonly="true" required="required" 
			data-options="label:'编号：',width:270,labelAlign:'right'"><br/> -->
		</p>

		<p>
				<select id="zt" class="easyui-combobox" panelHeight='auto'
					data-options="label:'状态：',labelAlign:'right',width:250,editable:false">
					<option selected="selected">在职 </option>
					<option >离职</option>
				</select>
			
				<input id="phone" class="easyui-textbox"  required="required"
				data-options="label:'电话：',width:250,validType:'brdh',labelAlign:'right'">
			
		</p>
		
		<p>		
				<select class="easyui-combobox" id="xgbumen" panelHeight='auto'
					data-options="label:'部门：',labelAlign:'right',width:250,editable:false">
					<option value="门诊" selected="selected">门诊</option>
					<option value="住院">住院</option>
				</select>
				
				<select class="easyui-combobox" id="xzksa" panelHeight='auto'
					data-options="label:'科室：',labelAlign:'right',width:250,editable:false">
				</select>
		</p>
		<p>
				
		</p>
				<a class='easyui-linkbutton' id="xgxx"style="margin-left:350px;">确认</a>
 				<a class='easyui-linkbutton' id="xiugqx"style="margin-left:10px;">取消</a>
		</form>
		
	</div>
	<div id="xgmm" class="easyui-window" title="修改密码" style="width:500px;height:300px"
	    	data-options="iconCls:'icon-save',closed:true">
		<form id="xgmma">
		<p>
			<input id="names" class="easyui-textbox" readonly="true" required="required"
									data-options="label:'姓名：',width:270,labelAlign:'right'"><br/><br/>
		</p>


		<p>
				<input id="pwd"class="easyui-passwordbox" required="required"
									data-options="label:'原密码：',width:270,labelAlign:'right'"><br/><br/>
		</p>
		<p>		
				<input id="pwds" class="easyui-passwordbox"  required="required"
									data-options="label:'新密码：',width:270,labelAlign:'right'"><br/><br/>
	</p>
			<a id="qrxgmm" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">确定</a>

		</form>
	</div>
		<div class="easyui-window" id="grant-win" title="给用户授角色" style="width:350px;height:500px;" data-options="closed:true">
		<div class="easyui-layout" data-options="fit:true">
			<div data-options="region:'south',border:false" style="height:30px;text-align:right">
				<a id="save-grant" class="easyui-linkbutton" >保存</a>
				<a id="close-grant" class="easyui-linkbutton" >取消</a>
			</div>
			<div data-options="region:'center'">
				<ul class="easyui-tree" id="grant-tree"></ul>
			</div>
	    </div>
		</div>
	</div>
</body>
</html>